#!/bin/bash -e

if [ $# == 0 ] ; then
   echo "please input params, hk(香港)，jp(日本)，ag(美国)，am（澳门）,sk(韩国)"
   exit 0
fi

sudo rm -rf /docker/v2ray

sudo mkdir -p /docker/v2ray

echo "---created /docker/v2ray/"

sudo mkdir -p /docker/v2ray/logs

echo "---created /docker/v2ray/logs"

echo "check Docker......"
sudo docker -v
if [ $? -eq  0 ]; then
    echo "检查到Docker已安装!"
else
    echo "安装docker环境..."
    sudo yum install -y yum-utils device-mapper-persistent-data
    sudo yum-config-manager --add-repo  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    sudo yum install docker-ce-18.06.1*
    echo "安装docker环境...安装完成!"
fi


echo "---start pull v2ray/official----"
sudo docker login registry.cn-shanghai.aliyuncs.com
sudo docker pull registry.cn-shanghai.aliyuncs.com/automsen/v2ray:4.31.0
echo "---end ------"

case "$1" in 

    hk) 
	echo "---installing 香港----"
	;;
    jp)
	echo "---installing 日本----"
	;;
    ag)
	echo "---installing 美国----"
	;;
    am)
        echo "---installing 澳门----"
	;;
    tw)
        echo "---installing 台湾----"
	;;
    sk)
        echo "---installing 韩国----"
	;;
    *)
	echo "-------------other"
	;;
esac

sudo wget -P /docker/v2ray  https://gitee.com/t_deploy/shell_v2/raw/master/v2config/config_$1.json
sudo mv /docker/v2ray/config_$1.json /docker/v2ray/config.json

if [ ! -d "/docker/v2ray/config.json" ];then
   	sudo chmod -R 776 /docker/v2ray/config.json
   else
	exit 0
fi

echo "---start run docker----"
set +e
docker stop v2ray
docker rm v2ray
set -e

docker run -d --net host --name v2ray \
--restart=always \
-v /docker/v2ray:/etc/v2ray \
-v /docker/v2ray/logs:/var/log/v2ray \
--restart=always registry.cn-shanghai.aliyuncs.com/automsen/v2ray:4.31.0
echo "---start sccuess-------"
